import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';

import '../../generated/locales.g.dart';

part 'user.g.dart';

@JsonSerializable()
class CurrentUser {
  final int? id;

  @JsonKey(name: 'jwt_token')
  final String? jwtToken;

  String? name;

  final int? cc;
  final String? mobile;

  @JsonKey(name: 'union_id')
  final String? unionId;

  @JsonKey(name: 'open_id')
  final String? openId;

  String? avatar;

  int? gender; //男1 女2

  String? height;

  String? weight;

  String? birthday;

  final String? email;
  String? city;

  CurrentUser({
    this.id,
    this.jwtToken,
    this.name,
    this.cc,
    this.mobile,
    this.unionId,
    this.openId,
    this.avatar,
    this.gender,
    this.height,
    this.weight,
    this.birthday,
    this.email,
    this.city,
  });

  factory CurrentUser.fromJson(Map<String, dynamic> json) =>
      _$CurrentUserFromJson(json);

  Map<String, dynamic> toJson() => _$CurrentUserToJson(this);

  String? getGenderString() {
    if (gender != null) {
      if (gender == 1) {
        return LocaleKeys.male.tr;
      } else if (gender == 2) {
        return LocaleKeys.female.tr;
      }
    }
    return null;
  }

  bool isBindWx() {
    return unionId != null && unionId!.isNotEmpty;
  }

  bool isAddEmail() {
    return email != null && email!.isNotEmpty;
  }
}

@JsonSerializable()
class AccountInfo {
  final int cc;

  AccountInfo({
    required this.cc,
  });

  factory AccountInfo.fromJson(Map<String, dynamic> json) =>
      _$AccountInfoFromJson(json);

  Map<String, dynamic> toJson() => _$AccountInfoToJson(this);
}

@JsonSerializable()
class UserTargetItem {
  final double min;
  final double max;
  final double value;

  UserTargetItem({
    required this.min,
    required this.max,
    required this.value,
  });

  UserTargetItem copyWith({
    double? min,
    double? max,
    double? value,
  }) {
    return UserTargetItem(
      min: min ?? this.min,
      max: max ?? this.max,
      value: value ?? this.value,
    );
  }

  factory UserTargetItem.fromJson(Map<String, dynamic> json) =>
      _$UserTargetItemFromJson(json);

  Map<String, dynamic> toJson() => _$UserTargetItemToJson(this);
}

@JsonSerializable(explicitToJson: true)
class UserTargetSetting {
  /// 走路
  final UserTargetItem step;

  /// 情绪舒缓
  final UserTargetItem emo;

  /// 目标睡眠时
  final UserTargetItem hour;

  /// 目标睡眠分
  final UserTargetItem minute;

  /// 低压力
  final UserTargetItem stress;

  UserTargetSetting({
    required this.step,
    required this.emo,
    required this.hour,
    required this.minute,
    required this.stress,
  });

  UserTargetSetting copyWith({
    UserTargetItem? step,
    UserTargetItem? emo,
    UserTargetItem? hour,
    UserTargetItem? minute,
    UserTargetItem? stress,
  }) {
    return UserTargetSetting(
      step: step ?? this.step,
      emo: emo ?? this.emo,
      hour: hour ?? this.hour,
      minute: minute ?? this.minute,
      stress: stress ?? this.stress,
    );
  }

  factory UserTargetSetting.fromJson(Map<String, dynamic> json) =>
      _$UserTargetSettingFromJson(json);

  Map<String, dynamic> toJson() => _$UserTargetSettingToJson(this);
}

@JsonSerializable(explicitToJson: true)
class UserRelationResponse {
  final int code;
  final String msg;
  final List<UserRelation> data;

  UserRelationResponse({
    required this.code,
    required this.msg,
    required this.data,
  });

  factory UserRelationResponse.fromJson(Map<String, dynamic> json) =>
      _$UserRelationResponseFromJson(json);

  Map<String, dynamic> toJson() => _$UserRelationResponseToJson(this);
}

@JsonSerializable()
class UserRelation {
  final int id;
  final String name;
  @JsonKey(name: 'zh_name')
  final String zhName;
  @JsonKey(name: 'en_name')
  final String enName;

  UserRelation({
    required this.id,
    this.name = '',
    required this.zhName,
    required this.enName,
  });

  factory UserRelation.fromJson(Map<String, dynamic> json) =>
      _$UserRelationFromJson(json);

  Map<String, dynamic> toJson() => _$UserRelationToJson(this);
}

@JsonSerializable(explicitToJson: true)
class EmergencyContactListResponse {
  final int code;
  final String msg;
  final List<EmergencyContact> data;

  EmergencyContactListResponse({
    required this.code,
    required this.msg,
    required this.data,
  });

  factory EmergencyContactListResponse.fromJson(Map<String, dynamic> json) =>
      _$EmergencyContactListResponseFromJson(json);

  Map<String, dynamic> toJson() => _$EmergencyContactListResponseToJson(this);
}

@JsonSerializable(explicitToJson: true)
class EmergencyContactResponse {
  final int code;
  final String msg;
  final EmergencyContact data;

  EmergencyContactResponse({
    required this.code,
    required this.msg,
    required this.data,
  });

  factory EmergencyContactResponse.fromJson(Map<String, dynamic> json) =>
      _$EmergencyContactResponseFromJson(json);

  Map<String, dynamic> toJson() => _$EmergencyContactResponseToJson(this);
}

@JsonSerializable(explicitToJson: true)
class EmergencyContact {
  int id;
  @JsonKey(name: 'user_id')
  int? userId;
  String type;
  String name;
  String cc;
  String mobile;
  int notify;
  UserRelation? relation;

  EmergencyContact({
    required this.id,
    this.userId,
    required this.type,
    required this.name,
    required this.cc,
    required this.mobile,
    required this.notify,
    this.relation,
  });

  factory EmergencyContact.fromJson(Map<String, dynamic> json) =>
      _$EmergencyContactFromJson(json);

  Map<String, dynamic> toJson() => _$EmergencyContactToJson(this);
}

@JsonSerializable(explicitToJson: true)
class FamilyMemberListResponse {
  final int code;
  final String msg;
  final List<FamilyMember> data;

  FamilyMemberListResponse({
    required this.code,
    required this.msg,
    required this.data,
  });

  factory FamilyMemberListResponse.fromJson(Map<String, dynamic> json) =>
      _$FamilyMemberListResponseFromJson(json);

  Map<String, dynamic> toJson() => _$FamilyMemberListResponseToJson(this);
}

@JsonSerializable(explicitToJson: true)
class FamilyMember {
  int id;
  @JsonKey(name: 'user_id')
  int? userId;
  int? relation_id;
  String? account;
  String? remark;
  String? mac;
  String? verificationCode;
  String? user_avatar;
  @JsonKey(name: 'relation_zh_name')
  final String? relationZhName;
  @JsonKey(name: 'relation_name')
  final String? relationName;
  @JsonKey(name: 'relation_en_name')
  final String? relationEnName;
  @JsonKey(name: 'smart_ring')
  final FamilyMemberSmartRing? smartRing;

  FamilyMember({
    required this.id,
    this.userId,
    this.relationZhName,
    this.relationName,
    this.relationEnName,
    this.mac,
    this.verificationCode,
    this.smartRing,
    this.relation_id,
    this.account,
    this.remark,
    this.user_avatar,
  });

  factory FamilyMember.fromJson(Map<String, dynamic> json) =>
      _$FamilyMemberFromJson(json);

  Map<String, dynamic> toJson() => _$FamilyMemberToJson(this);
}

@JsonSerializable(explicitToJson: true)
class FamilyMemberSmartRing {
  final int? id;
  final String name;
  final String? address;
  @JsonKey(name: 'bind_time')
  final int bindTime;
  final int battery;
  @JsonKey(name: 'wear_status')
  final int wearStatus;

  FamilyMemberSmartRing({
    this.id,
    required this.name,
    required this.address,
    required this.bindTime,
    required this.battery,
    required this.wearStatus,
  });

  factory FamilyMemberSmartRing.fromJson(Map<String, dynamic> json) =>
      _$FamilyMemberSmartRingFromJson(json);

  Map<String, dynamic> toJson() => _$FamilyMemberSmartRingToJson(this);
}
